import { _decorator, Component } from 'cc';
import { computed, effect, EffectScope, ref, watch, WatchHandle } from '../../reactivity';
import { core_viewmodel } from '../viewModel/core_viewmodel';
import { vmManager } from '../viewModel/viewmodel_manage';
const { ccclass, property } = _decorator;

/**
 * View基类 - MVVM架构中的视图组件
 * 负责UI展示和用户交互
 */
@ccclass('core_view')
export class core_view extends Component {
    // 关联的ViewModel
    protected _viewModel: core_viewmodel | null = null;
    protected _watchHandle: WatchHandle | null = null;
    // 效果作用域

    protected onLoad(): void {
        this.onInit();
    }

    protected onInit(): void {
        this.init();
    }

    protected init(): void {
        this.initializeViewModel(core_viewmodel);
    }

    protected start(): void {
        this.onStart();
    }

    protected onStart(): void {
        
    }

    update(deltaTime: number) {
        // 子类可以重写

    }

    /**
     * 初始化ViewModel
     */
    protected initializeViewModel(viewModelConstructor: new () => core_viewmodel): void {
        this._viewModel = vmManager.createViewModel(this, viewModelConstructor);
    }

    /**
     * 获取ViewModel
     */
    public getViewModel(): core_viewmodel | null {
        return this._viewModel;
    }

    /**
     * 销毁View
     */
    onDestroy(): void {
        this._watchHandle?.();
        if (this._viewModel) {
            this._viewModel.destroy();
            this._viewModel = null;
        }
    }
}


